/**
 * @author SeuCS沈子怡(林昊天)Or(71119126严伟)
 * @desc 围住神经猫的界面搭建以及逻辑实现,2022.07.12
 */

class Main extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.once( egret.Event.ADDED_TO_STAGE, this.onAddToStage, this );
    }

    private onAddToStage(event:egret.Event) {
        //先加载背景
        var image:egret.ImageLoader=new egret.ImageLoader();
        image.once(egret.Event.COMPLETE,this.loadbackground,this);
        image.load("resource/bg.jpg");
        //再加载两只猫
        this.loadstay(this.initMovieClip);
        this.loadweizhu(this.initWeizhClip);
        //由于加载资源的并发性，可能导致后面点阵加载的时候猫未加载，但是点阵加载中会调用猫，所以设置等待10ms再加载点阵
        setTimeout(() => {
            var pot1load:egret.ImageLoader=new egret.ImageLoader();
            pot1load.once(egret.Event.COMPLETE, this.loadpot1, this);
            pot1load.load("resource/pot1.png");
            //加载失败、再来一局、分享的图标
            this.failure();
            this.playagain();
            this.share();
        //this.success();
        }, 10);  
    }
    private background:egret.Bitmap=new egret.Bitmap();//背景图片
    private fail:egret.Bitmap=new egret.Bitmap();//失败展示的图片
    private succ:egret.Bitmap=new egret.Bitmap();//成功展示的图片
    private repla:egret.Bitmap=new egret.Bitmap();//再来一局图标
    private shareBtn:egret.Bitmap=new egret.Bitmap();//分享图标
    //private cat:egret.Bitmap=new egret.Bitmap();
    private cat:egret.MovieClip;//正常猫
    private weizhucat:egret.MovieClip;//被围住的猫
    private textsucc:egret.TextField=new egret.TextField();//成功的文本信息
    private textfail:egret.TextField=new egret.TextField();//失败的文本信息
    private isObstacle:Array<Array<number>>;//障碍
    private state:boolean=null;//success、failure、unknown
    private timer:egret.Timer;//计时器
    private seconds:number=0;//初始时记为0s
    private row:number=4;//猫所在的行
    private col:number=4;//猫所在的列
    private start:egret.Bitmap;//开始按钮
    private dots:Array<Array<egret.Bitmap>>;//空闲点矩阵
    private pots:Array<Array<egret.Bitmap>>;//占位点矩阵
    private map:Array<Array<number>>=new Array<Array<number>>();//map矩阵，用于存储距离出口的最短距离
    private changeflag:number;//change标识符，判断回溯之后map有无更新
    //未被围住的猫的movieclip
    private _stayData:any;
    private _stayTexture:egret.Texture;
    //被围住的猫的movieclip
    private _weizhuData:any;
    private _weizhuTexture:egret.Texture;
   
    private init():void {//初始化
        //各种按钮设为不可视，state设为null，秒数置0，猫的初始位置在点阵中央
        this.repla.visible=false;
        this.fail.visible=false;
        this.textfail.visible=false;
        this.shareBtn.visible=false;
        this.state=null;
        this.seconds=0;
        this.row=4;
        this.col=4;
        //map初始化为全0
        for(let i:number=0;i<9;i++){
            var tmp:Array<number>=new Array<number>();
            this.map.push(tmp);
            for(let j:number=0;j<9;j++){
                this.map[i].push(0);
            }
        }
        //初始化时灰色点阵全部可视，橙色点阵全部不可视
        for(let i:number=0;i<9;i++){
            for(let j:number=0;j<9;j++){
                this.dots[i][j].visible=true;
                //this.dots[i][j].touchEnabled =true;
                this.pots[i][j].visible=false;
            }
        }
        this.isObstacle=new Array<Array<number>>();
        //障碍点全部置0，表示初始无障碍
        for(let i:number=0;i<9;i++){
            var tmp:Array<number>=new Array<number>();
            this.isObstacle.push(tmp);
            for(let j:number=0;j<9;j++){
                this.isObstacle[i].push(0);
            }
        }
        //在中心点方圆3步内(相对于矩阵而非真实排列)随机设置五个障碍点
        for(let i:number=0;i<5;i++){
            var x:number=Math.ceil(Math.random()*3-1);
            var y:number=Math.ceil(Math.random()*3-1);
            if(x==0&&y==0){
                i=i-1;
            }else{
                this.isObstacle[4+x][4+y]=1;//障碍点设置之后更细障碍矩阵
                this.dots[4+x][4+y].visible=false;//被设置障碍点的位置的灰色点不可视
                this.pots[4+x][4+y].visible=true;//被设置障碍点的位置橙色点可视
            }
        }
        //在图中随机选取8个位置设置障碍点
        for(let i:number=0;i<8;i++){
            var x:number=Math.ceil(Math.random()*9-1);
            var y:number=Math.ceil(Math.random()*9-1);
            if((x==4&&y==4)||this.isObstacle[x][y]==1)
            {
                i=i-1;
            }
            else{
                this.isObstacle[x][y]=1;
                this.dots[x][y].visible=false;
                this.pots[x][y].visible=true;
            }
        }
    }
    private loadbackground(eback:egret.Event):void{//加载背景图片
        var t1:egret.Texture=new egret.Texture();
        t1.bitmapData=eback.currentTarget.data;
        this.background.texture=t1;
        this.background.width=720;
        this.background.height=1280;
        this.background.visible=true;
        this.stage.addChild(this.background);
    }
    private loadstartbtn(evt:egret.Event):void{
        var t1:egret.Texture=new egret.Texture();
            t1.bitmapData=evt.currentTarget.data;
            this.start=new egret.Bitmap(t1);
            this.start.anchorOffsetX=this.start.width/2;
            this.start.anchorOffsetY=this.start.height/2;
            this.start.scaleX=1.5;
            this.start.scaleY=1.5;
            this.start.x=360;
            this.start.y=700;
            this.start.touchEnabled=true;
            this.stage.addChild(this.start);
            //开始游戏按钮的鼠标抬起监听事件
            this.start.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
                this.timer=new egret.Timer(100,0);//加载一个计时器监听花费的时间
                this.timer.addEventListener(egret.TimerEvent.TIMER,()=>{
                    this.seconds+=1;
                    //console.log("用时"+(this.seconds)/10+"s");
                    if(this.state==true||this.state==false)
                    {
                        this.seconds-=1;
                        this.timer.stop();
                    }
                },this);
                for(let ii:number=0;ii<9;ii++){
                    for(let jj:number=0;jj<9;jj++){
                        this.dots[ii][jj].touchEnabled=true;
                    }
                }
                this.timer.start();
                this.start.visible=false;//鼠标抬起后开始游戏按钮不可视，进入游戏开始计时
            },this);
    }
    private loadpot1(evt:egret.Event):void{
        var x:number;
        var y:number;
        var offset:number=18;
        this.dots=new Array<Array<egret.Bitmap>>();
        
        for(let i:number=0;i<9;i++){
            var off:number=(i%2)==1?offset:-offset;
            y=500+46+i*76;
            var tmp:Array<egret.Bitmap>=new Array<egret.Bitmap>();
            this.dots.push(tmp);
            for(let j:number=0;j<9;j++){
                x=10+46+j*76+off;
                //var shape:egret.Shape=new egret.Shape();
                var t1:egret.Texture=new egret.Texture();
                t1.bitmapData=evt.currentTarget.data;
                var bg:egret.Bitmap=new egret.Bitmap(t1);
                bg.anchorOffsetX=bg.width/2;
                bg.anchorOffsetY=bg.height/2;
                bg.x=x;
                bg.y=y;
                bg.visible=true;
                bg.touchEnabled=false;
                bg.scaleX=1.6;
                bg.scaleY=1.6;
                this.dots[i].push(bg);
                this.stage.addChildAt(this.dots[i][j],2);
                this.dots[i][j].addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
                    this.dots[i][j].visible=false;//被点击的灰色点不可视
                    this.pots[i][j].visible=true;//被点击的点对应位置的橙色点可视
                    this.isObstacle[i][j]=1;//被点击的点设置为障碍点
                    this.initmap();//每一步走完都将map初始化，用来动态计算每一步后的最短距离
                    this.recurshort();//对map每一个点的最短距离进行回溯计算
                    var tmpstate:number=this.move();//计算现在应该怎么走并返回相应的状态(走距离出口最短的)
                    if(tmpstate==-1){//状态-1代表没抓住
                        this.textfail.visible=true;
                        this.fail.visible=true;
                        this.repla.visible=true;
                        this.shareBtn.visible=true;
                        this.state=false;//state为false，停止计时
                        this.cat.visible=false;
                        this.weizhucat.visible=false;
                        //this.removeChild(this.cat);
                        for(let ii:number=0;ii<9;ii++){
                            for(let jj:number=0;jj<9;jj++){
                                this.dots[ii][jj].touchEnabled=false;
                            }
                        }
                    }else if(tmpstate==0 ){//状态为0代表成功围住
                        this.success();//加载一遍success所需素材
                        this.textsucc.visible=true;
                        this.succ.visible=true;
                        this.repla.visible=true;
                        this.shareBtn.visible=true;
                        this.state=true;//state为true，停止计时
                        this.cat.visible=false;//猫不可视
                        this.weizhucat.visible=false;
                        //this.removeChild(this.cat);
                        for(let ii:number=0;ii<9;ii++){
                            for(let jj:number=0;jj<9;jj++){
                                this.dots[ii][jj].touchEnabled=false;//游戏结束后点阵不可触
                            }
                        }
                    }else{
                        if(tmpstate==1){//上
                            this.row-=1;
                        }else if(tmpstate==2){//下
                            this.row+=1;
                        }else if(tmpstate==3){//左
                            this.col-=1;
                        }else if(tmpstate==4){//右
                            this.col+=1;
                        }else if(tmpstate==5){
                            if(this.row%2==0){
                                //奇数行左上
                                this.row-=1;
                                this.col-=1;
                            }else{
                                //偶数行右上
                                this.row-=1;
                                this.col+=1;
                            }
                        }else if(tmpstate==6){
                            if(this.row%2==0){
                                //奇数行左下
                                this.row+=1;
                                this.col-=1;
                            }else{
                                //偶数行右下
                                this.row+=1;
                                this.col+=1;
                            }
                        }
                        console.log("当前神经猫的坐标："+"("+this.row+","+this.col+")");
                        //神经猫的坐标改变之后对应两只猫的图像也要随之改变
                        this.cat.x=this.dots[this.row][this.col].x;
                        this.cat.y=this.dots[this.row][this.col].y-50;
                        this.weizhucat.x=this.dots[this.row][this.col].x;
                        this.weizhucat.y=this.dots[this.row][this.col].y-50;
                    } 
                    //console.log(this.short.length);
                    //console.log(this.path.length);
                },this);
            }
        }
        //灰色点阵加载好之后加载橙色点阵
        var pot2load:egret.ImageLoader=new egret.ImageLoader;
        pot2load.once(egret.Event.COMPLETE, this.loadpot2, this);
        pot2load.load("resource/pot2.png");
    }
    private loadpot2(evt:egret.Event):void{
        this.stage.addChild(this.cat);
        this.stage.addChild(this.weizhucat);
        var x:number;
        var y:number;
        var offset:number=18;
        this.pots=new Array<Array<egret.Bitmap>>();
        for(let i:number=0;i<9;i++){
            var off:number=(i%2)==1?offset:-offset;
            y=500+46+i*76;
            var tmp:Array<egret.Bitmap>=new Array<egret.Bitmap>();
            this.pots.push(tmp);
            for(let j:number=0;j<9;j++){
                x=10+46+j*76+off;
                //var shape:egret.Shape=new egret.Shape();
                var t1:egret.Texture=new egret.Texture();
                t1.bitmapData=evt.currentTarget.data;
                var bg:egret.Bitmap=new egret.Bitmap(t1);
                bg.anchorOffsetX=bg.width/2;
                bg.anchorOffsetY=bg.height/2;
                bg.x=x;
                bg.y=y;
                bg.visible=false;//障碍点初始为不可视，后续根据初始化以及界面交互设置其可视
                bg.touchEnabled=false;//障碍点不可点击
                bg.scaleX=1.6;
                bg.scaleY=1.6;
                this.pots[i].push(bg);//障碍点存储在二维数组
                this.stage.addChildAt(this.pots[i][j],2);
            }
        }
        //点阵加载完毕之后将两种状态的猫放在指定位置，一个猫可视一个不可视(逃不出去的那只不可视)
        this.cat.visible=true;
        this.cat.x=this.dots[this.row][this.col].x;
        this.cat.y=this.dots[this.row][this.col].y-50;
        this.weizhucat.visible=false;
        this.weizhucat.x=this.dots[this.row][this.col].x;
        this.weizhucat.y=this.dots[this.row][this.col].y-50;
        //点阵初始化
        this.init();
        //导入开始按钮
        var startbtn:egret.ImageLoader=new egret.ImageLoader();
        startbtn.once(egret.Event.COMPLETE, this.loadstartbtn, this);
        startbtn.load("resource/btn_start.png");
    }
    private success():void{
        //成功状态
        var imgLoader:egret.ImageLoader = new egret.ImageLoader;
        imgLoader.once( egret.Event.COMPLETE, ()=>{
            var t1:egret.Texture=new egret.Texture();
            t1.bitmapData=imgLoader.data;
            this.succ.texture=t1;
            this.succ.anchorOffsetX=this.succ.width/2;//改变锚点，将其(x,y)坐标(0,0)点设置为bitmap的中心位置(白鹭引擎默认00是左上角)
            this.succ.anchorOffsetY=this.succ.height/2;
            this.succ.x=360;
            this.succ.y=600;
            this.succ.scaleX=1.4;//scale用来调整图像大小，x表示水平放缩，y表示竖直放缩
            this.succ.scaleY=1.4;
            this.succ.visible=true;
            this.stage.addChild(this.succ);
            //成功时的文本信息
            this.textsucc.size=50;
            this.textsucc.x=360;
            this.textsucc.y=670;
            this.textsucc.textAlign=egret.HorizontalAlign.LEFT;
            this.textsucc.textColor = 0xfffff0;
            this.textsucc.type = egret.TextFieldType.DYNAMIC;
            this.textsucc.lineSpacing = 10;
            this.textsucc.wordWrap = true;
            this.textsucc.multiline = true;
            this.textsucc.stroke=5;//描边宽度设置为5
            this.textsucc.visible=true;
            this.textsucc.width = this.succ.width - 100;
            this.textsucc.text="恭喜你抓住了神经猫，用时"+(this.seconds)/10+"s";
            this.textsucc.anchorOffsetX=this.textsucc.width/2;
            this.textsucc.anchorOffsetY=this.textsucc.height/2;
            this.stage.addChild(this.textsucc);
        }, this );
        imgLoader.load( "resource/victory.png" );
    }
    private failure():void{
        //失败状态
        var imgLoader:egret.ImageLoader = new egret.ImageLoader;
        imgLoader.once( egret.Event.COMPLETE, ()=>{
            var t1:egret.Texture=new egret.Texture();
            t1.bitmapData=imgLoader.data;
            this.fail.texture=t1;
            this.fail.anchorOffsetX=this.fail.width/2;
            this.fail.anchorOffsetY=this.fail.height/2;
            this.fail.x=360;
            this.fail.y=580;
            this.fail.visible=false;//初始为不可视
            this.fail.scaleX=1.4;
            this.fail.scaleY=1.4;
            this.stage.addChild(this.fail);
            //fail时的文本信息
            this.textfail.size=40;
            this.textfail.x=360;
            this.textfail.y=670;
            this.textfail.textAlign=egret.HorizontalAlign.LEFT;
            this.textfail.textColor = 0xfffff0;
            this.textfail.type = egret.TextFieldType.DYNAMIC;
            this.textfail.lineSpacing = 10;
            this.textfail.wordWrap = true;
            this.textfail.multiline = true;
            this.textfail.visible=false;//初始为不可视
            this.textfail.width = this.fail.width - 50;
            //this.textfail.border=true;
            this.textfail.stroke=5;
            this.textfail.text="你没有抓住神！经！猫！！"+"\n 精神院院长又发神经病了！";
            this.textfail.anchorOffsetX=this.textfail.width/2;
            this.textfail.anchorOffsetY=this.textfail.height/2;
            this.stage.addChild(this.textfail);
        }, this );
        imgLoader.load( "resource/failed.png" );
    }
    private playagain():void{
        var imgLoader:egret.ImageLoader = new egret.ImageLoader;
        imgLoader.once( egret.Event.COMPLETE, ()=>{
            var t1:egret.Texture=new egret.Texture();
            t1.bitmapData=imgLoader.data;
            this.repla.texture=t1;
            this.repla.touchEnabled=true;
            this.repla.anchorOffsetX=this.repla.width/2;
            this.repla.anchorOffsetY=this.repla.height/2;
            this.repla.scaleX=1.4;
            this.repla.scaleY=1.4;
            this.repla.x=520;
            this.repla.y=880;
            this.repla.visible=false;//初始为不可视
            //再来一次的鼠标抬起监听，对点阵以及各界面进行初始化
            this.repla.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
                this.init();
                this.cat.visible=true;
                this.cat.x=this.dots[this.row][this.col].x;
                this.cat.y=this.dots[this.row][this.col].y-50;
                console.clear();
                this.succ.visible=false;
                //this.stage.removeChild(this.succ);
                //this.stage.removeChild(this.textsucc);
                this.textsucc.visible=false;
                this.timer=new egret.Timer(100,0);
                //新增一个计时器监听，记录新一轮游戏的时间
                this.timer.addEventListener(egret.TimerEvent.TIMER,()=>{
                    this.seconds+=1;
                    //console.log("用时"+(this.seconds)/10+"s");
                    if(this.state==true||this.state==false)
                    {//成功或失败后state为true或null，计时器停止计时，正常状态state设置为null
                        this.seconds-=1;
                        this.timer.stop();
                    }
                },this);
                //再来一次后所有的灰色点可触(灰色点的监听中设置成功或失败后其不可触，这里再将其设置为可触以便新一轮游戏)
                for(let ii:number=0;ii<9;ii++){
                    for(let jj:number=0;jj<9;jj++){
                        this.dots[ii][jj].touchEnabled=true;
                    }
                }
                this.timer.start();//启动计时器
            },this);
            this.stage.addChild(this.repla);
        },this);
        imgLoader.load( "resource/replay.png" );
    }
    private share():void{//将share按钮导入到场景中
        var imgLoader:egret.ImageLoader = new egret.ImageLoader;
        imgLoader.once( egret.Event.COMPLETE, ()=>{
            var t1:egret.Texture=new egret.Texture();
            t1.bitmapData=imgLoader.data;
            this.shareBtn.texture=t1;
            this.shareBtn.touchEnabled=false;
            this.shareBtn.anchorOffsetX=this.shareBtn.width/2;
            this.shareBtn.anchorOffsetY=this.shareBtn.height/2;
            this.shareBtn.scaleX=1.4;
            this.shareBtn.scaleY=1.4;
            this.shareBtn.x=200;
            this.shareBtn.visible=false;
            this.shareBtn.y=880;
            this.stage.addChild(this.shareBtn);
        },this);
        imgLoader.load( "resource/shareBTN.png" );
    }
    private initmap():void{//初始化map图，将边缘点设置为1，正常点设置为0，障碍点设置为-1
        for(let i:number=0;i<9;i++){
            for(let j:number=0;j<9;j++){
                this.map[i][j]=0;
            }
        }
        this.changeflag=0;
        for(let i:number=0;i<9;i++){
            for(let j:number=0;j<9;j++){
                if(this.isObstacle[i][j]==1)
                {
                    this.map[i][j]=-1;
                }
            }
        }
        for(let i:number=0;i<9;i++){
            if(this.isObstacle[i][0]==0){
                this.map[i][0]=1;
            }
            if(this.isObstacle[i][8]==0){
                this.map[i][8]=1;
            }
            if(this.isObstacle[8][i]==0){
                this.map[8][i]=1;
            }
            if(this.isObstacle[0][i]==0){
                this.map[0][i]=1;
            }
        }
    }
    private recurshort():void{//回溯进行map最短距离的更新
        this.changeflag=0;
        for(let i:number=1;i<8;i++){
            for(let j:number=1;j<8;j++){
                //上
                if(this.map[i][j-1]>0&&(this.map[i][j-1]<this.map[i][j]-1||this.map[i][j]==0)){
                    this.changeflag=1;
                    this.map[i][j]=this.map[i][j-1]+1;
                }
                //下
                if(this.map[i][j+1]>0&&(this.map[i][j+1]<this.map[i][j]-1||this.map[i][j]==0)){
                    this.changeflag=1;
                    this.map[i][j]=this.map[i][j+1]+1;
                }
                //左
                if(this.map[i-1][j]>0&&(this.map[i-1][j]<this.map[i][j]-1||this.map[i][j]==0)){
                    this.changeflag=1;
                    this.map[i][j]=this.map[i-1][j]+1;
                }
                //右
                if(this.map[i+1][j]>0&&(this.map[i+1][j]<this.map[i][j]-1||this.map[i][j]==0)){
                    this.changeflag=1;
                    this.map[i][j]=this.map[i+1][j]+1;
                }
                if(i%2==0){
                    //奇数行,左上(i-1,j-1)左下(i+1,j-1)
                    //左上
                    if(this.map[i-1][j-1]>0&&(this.map[i-1][j-1]<this.map[i][j]-1||this.map[i][j]==0)){
                        this.changeflag=1;
                        this.map[i][j]=this.map[i-1][j-1]+1;
                    }
                    //左下
                    if(this.map[i+1][j-1]>0&&(this.map[i+1][j-1]<this.map[i][j]-1||this.map[i][j]==0)){
                        this.changeflag=1;
                        this.map[i][j]=this.map[i+1][j-1]+1;
                    }
                }else{
                    //偶数行，右上(i-1,j+1)右下(i+1,j+1)
                    //右上
                    if(this.map[i-1][j+1]>0&&(this.map[i-1][j+1]<this.map[i][j]-1||this.map[i][j]==0)){
                        this.changeflag=1;
                        this.map[i][j]=this.map[i-1][j+1]+1;
                    }
                    //右下
                    if(this.map[i+1][j+1]>0&&(this.map[i+1][j+1]<this.map[i][j]-1||this.map[i][j]==0)){
                        this.changeflag=1;
                        this.map[i][j]=this.map[i+1][j+1]+1;
                    }
                }
            }
        }
        if(this.changeflag==1)
        {
            //changeflag为1说明距离矩阵改变，需要回溯
            this.recurshort();
        }
    }
    private move():number{
        //已经走在边界了，成功逃脱
        if(this.row==0||this.row==8||this.col==0||this.col==8){
            return -1;
        }
        var d1:number=this.map[this.row-1][this.col];//猫所处位置的上
        var d2:number=this.map[this.row+1][this.col];//猫所处位置的下
        var d3:number=this.map[this.row][this.col-1];//猫所处位置的左
        var d4:number=this.map[this.row][this.col+1];//猫所处位置的右
        if(this.row%2==0){
            //奇数行，左上左下
            var d5:number=this.map[this.row-1][this.col-1];//猫所处位置的左上
            var d6:number=this.map[this.row+1][this.col-1];//猫所处位置的左下
        }else{
            //偶数行，右上右下
            var d5:number=this.map[this.row-1][this.col+1];//猫所处位置的右上
            var d6:number=this.map[this.row+1][this.col+1];//猫所处位置的右上
        }
        var min:number=Math.min(d1,Math.min(d2,Math.min(d3,Math.min(d4,Math.min(d5,d6)))));
        var max:number=Math.max(d1,Math.max(d2,Math.max(d3,Math.max(d4,Math.max(d5,d6)))));
        if(max==0){//max为0说明已经被困住但是还可以走
            this.cat.visible=false;//困住后正常形态的猫不可视
            this.weizhucat.visible=true;//困住后被围住形态的猫可视
            if(max==d1){
                return 1;//上
            }else if(max==d2){
                return 2;//下
            }else if(max==d3){
                return 3;//左
            }else if(max==d4){
                return 4;//右
            }else if(max==d5){
                return 5;//左上或右上
            }else if(max==d6){
                return 6;//左下或右下
            }
        }else if(max==-1){//max为-1说明六个方向都是障碍，即成功围住
            return 0;
        }else if(min==1){//min为1说明猫已经走到了边缘，可以逃脱
            return -1;
        }
        if(d1==-1){
            d1=100;
        }
        if(d2==-1){
            d2=100;
        }
        if(d3==-1){
            d3=100;
        }
        if(d4==-1){
            d4=100;
        }
        if(d5==-1){
            d5=100;
        }
        if(d6==-1){
            d6=100;
        }
        min=Math.min(d1,Math.min(d2,Math.min(d3,Math.min(d4,Math.min(d5,d6)))));
        if(min==d1){
            return 1;//上
        }else if(min==d2){
            return 2;//下
        }else if(min==d3){
            return 3;//左
        }else if(min==d4){
            return 4;//右
        }else if(min==d5){
            return 5;//左上或右上
        }else if(min==d6){
            return 6;//左下或右下
        }
    }
    private initMovieClip():void {//正常形态的猫的movieclip展示
        var mcDataFactory = new egret.MovieClipDataFactory(this._stayData, this._stayTexture);
        this.cat= new egret.MovieClip(mcDataFactory.generateMovieClipData("stay"));
        this.cat.gotoAndPlay(1,-1);
        this.cat.anchorOffsetX=this.cat.width/2;
        this.cat.anchorOffsetY=this.cat.height/2;
        this.cat.x = 300;
        this.cat.y = 580;
        this.cat.visible=false;
        this.cat.scaleX=1.2;
        this.cat.scaleY=1.2;
    }
    protected loadstay(callback:Function):void {//正常形态的猫的movieclip加载
        var count:number = 0;
        var self = this;
        var check = function () {
            count++;
            if (count == 2) {
                callback.call(self);
            }
        }
        var loader = new egret.URLLoader();
        loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
            var loader = e.currentTarget;
            this._stayTexture = loader.data;
            check();
        }, this);
        loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        var request = new egret.URLRequest("resource/stay.png");
        loader.load(request);
        var loader = new egret.URLLoader();
        loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
            var loader = e.currentTarget;
            this._stayData = JSON.parse(loader.data);
            check();
        }, this);
        loader.dataFormat = egret.URLLoaderDataFormat.TEXT;
        var request = new egret.URLRequest("resource/stay.json");
        loader.load(request);
    }
    private initWeizhClip():void {//困住形态的猫的movieclip展示
        var mcDataFactory = new egret.MovieClipDataFactory(this._weizhuData, this._weizhuTexture);
        this.weizhucat= new egret.MovieClip(mcDataFactory.generateMovieClipData("weizhu"));
        //循环展示动作，第二个参数为-1
        this.weizhucat.gotoAndPlay(1,-1);
        this.weizhucat.anchorOffsetX=this.weizhucat.width/2;
        this.weizhucat.anchorOffsetY=this.weizhucat.height/2;
        this.weizhucat.x = 300;
        this.weizhucat.y = 600;
        this.weizhucat.visible=false;
        this.weizhucat.scaleX=1.2;
        this.weizhucat.scaleY=1.2;
    }
    protected loadweizhu(callback:Function):void {//困住形态的猫的movieclip加载
        var count:number = 0;
        var self = this;
        var check = function () {
            count++;
            if (count == 2) {
                callback.call(self);
            }
        }
        var loader = new egret.URLLoader();
        loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
            var loader = e.currentTarget;
            this._weizhuTexture = loader.data;          
            check();
        }, this);
        loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        var request = new egret.URLRequest("resource/weizhu.png");
        loader.load(request);       
        var loader = new egret.URLLoader();
        loader.addEventListener(egret.Event.COMPLETE, function loadOver(e) {
            var loader = e.currentTarget;
            this._weizhuData = JSON.parse(loader.data);
            check();
        }, this);
        loader.dataFormat = egret.URLLoaderDataFormat.TEXT;
        var request = new egret.URLRequest("resource/weizhu.json");
        loader.load(request);
    }
}